package com.biostime.springdraw.modules.sd.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.biostime.springdraw.common.service.CrudService;
import com.biostime.springdraw.modules.sd.dao.ShareDao;
import com.biostime.springdraw.modules.sd.dao.ShareawardDao;
import com.biostime.springdraw.modules.sd.dao.SharegridDao;
import com.biostime.springdraw.modules.sd.entity.Award;
import com.biostime.springdraw.modules.sd.entity.Grid;
import com.biostime.springdraw.modules.sd.entity.Section;
import com.biostime.springdraw.modules.sd.entity.Share;
import com.biostime.springdraw.modules.sd.entity.Shareaward;
import com.biostime.springdraw.modules.sd.entity.Sharegrid;
import com.biostime.springdraw.modules.sys.entity.User;

/**
 * 分享Service
 * @author chenweicheng
 * @version 2016-05-03
 */
@Service
@Transactional
public class ShareService extends CrudService<ShareDao, Share> {
		
	@Autowired
	private ShareawardDao shareawardDao;
	
	@Autowired
	private SharegridDao sharegridDao;
	
	@Autowired
	private SectionService sectionService;
	
	@Autowired
	private AwardService awardService;
	
	@Autowired
	private GridService gridService;

	
	@Override
	public void save(Share entity ) {
		if (entity.getIsNewRecord()){
			
			/**新增分享单**/
			entity.setCreateBy(new User("1"));
			entity.setUpdateBy(new User("1"));
			entity.setCreateDate(new Date()); 
			entity.setUpdateDate(new Date());  	
			
			Section sectionExm = new Section();
			sectionExm.setNow(new Date());
			List<Section> sections = sectionService.findList(sectionExm);		
			if(sections != null && !sections.isEmpty()){
				entity.setAwardSectionCode(sections.get(0).getCode()); 
			}else{
				entity.setAwardSectionCode("0"); 
			}					
			
			dao.insert(entity);		
					
			/**临时新增分享单奖品**/			
			List<Award> awardList = awardService.findList(new Award());
			Shareaward sa = null;
			if(awardList != null && !awardList.isEmpty()){
				for(Award award : awardList){
					if(award.getIsShare().equals("1")){
						sa = new Shareaward();
						sa.setShareId(Integer.parseInt(entity.getId()));
						sa.setAwardId(Integer.parseInt(award.getId())); 
						sa.setIsAward("0"); 
						sa.setCreateBy(new User("1"));
						sa.setUpdateBy(new User("1"));
						sa.setCreateDate(new Date());
						sa.setUpdateDate(new Date());
						shareawardDao.insert(sa);			 										
					}
				}
			}
			
			//无奖品				
			Shareaward a4 = new Shareaward();
			a4.setShareId(Integer.parseInt(entity.getId()));
			a4.setAwardId(0); 
			a4.setIsAward("0"); 
			a4.setCreateBy(new User("1"));
			a4.setUpdateBy(new User("1"));
			a4.setCreateDate(new Date());
			a4.setUpdateDate(new Date());					
			shareawardDao.insert(a4);		
			
			/**临时新增分享单格子**/
			List<Grid>  gridList = gridService.findList(new Grid());
			Sharegrid g1 = null;
			if(gridList != null && !gridList.isEmpty()){
				for(Grid grid:gridList){
					g1 = new Sharegrid();
					g1.setGridId(Integer.parseInt(grid.getId())); 
					g1.setIsExists("0"); 
					g1.setCreateBy(new User("1"));
					g1.setUpdateBy(new User("1"));
					g1.setCreateDate(new Date());
					g1.setUpdateDate(new Date());		
					g1.setShareId(Integer.parseInt(entity.getId()));
					sharegridDao.insert(g1);									
				}

			}
						
		}else{
			entity.setUpdateBy(new User("1"));
			entity.setUpdateDate(new Date());  			
			dao.update(entity);
		}
	}
	
	public List<Share> findShare(Share share){
		return dao.findShare(share);
	}

}
